import { compile } from "path-to-regexp";
import { request } from "../../data/define";
import { getURLSearchParams } from "./getURLSearchParams";
import { transCellToObj } from "./transCellToObj";

/** 转化对象为路径 */
export function getPath(request: request, defineRequest: any) {
    const [prot, path] = request.root.split(/(?<=^https?):\/\//);
    const toPath = compile(path);
    /** 写入路径参数 */
    const params = Object.assign(
        {},
        transCellToObj(request.pathParams),
        defineRequest.params
    );
    console.log(request.pathParams, params);
    let url = toPath(params);
    if (request.params && request.params.length) {
        /** 写入请求参数 */
        url +=
            "?" +
            getURLSearchParams(transCellToObj(request.params))?.toString();
    }
    return prot + "://" + url;
}
